package com.ruoyi.pv.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.pv.domain.PvBookingTimeInfo;
import com.ruoyi.pv.mapper.PvBookingTimeMapper;
import com.ruoyi.pv.domain.PvBookingTime;
import com.ruoyi.pv.service.IPvBookingTimeService;

/**
 * 预约时段类型Service业务层处理
 *
 * @author zzx
 * @date 2023-03-12
 */
@Service
public class PvBookingTimeServiceImpl implements IPvBookingTimeService {
    @Autowired
    private PvBookingTimeMapper pvBookingTimeMapper;

    /**
     * 查询预约时段类型
     *
     * @param id 预约时段类型主键
     * @return 预约时段类型
     */
    @Override
    public PvBookingTime selectPvBookingTimeById(Long id) {
        return pvBookingTimeMapper.selectPvBookingTimeById(id);
    }

    /**
     * 查询预约时段类型列表
     *
     * @param pvBookingTime 预约时段类型
     * @return 预约时段类型
     */
    @Override
    public List<PvBookingTime> selectPvBookingTimeList(PvBookingTime pvBookingTime) {
        return pvBookingTimeMapper.selectPvBookingTimeList(pvBookingTime);
    }

    /**
     * 新增预约时段类型
     *
     * @param pvBookingTime 预约时段类型
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPvBookingTime(PvBookingTime pvBookingTime) {
        pvBookingTime.setCreateTime(DateUtils.getNowDate());
        int rows = pvBookingTimeMapper.insertPvBookingTime(pvBookingTime);
        insertPvBookingTimeInfo(pvBookingTime);
        return rows;
    }

    /**
     * 修改预约时段类型
     *
     * @param pvBookingTime 预约时段类型
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePvBookingTime(PvBookingTime pvBookingTime) {
        pvBookingTimeMapper.deletePvBookingTimeInfoByTimeId(pvBookingTime.getId());
        insertPvBookingTimeInfo(pvBookingTime);
        return pvBookingTimeMapper.updatePvBookingTime(pvBookingTime);
    }

    /**
     * 批量删除预约时段类型
     *
     * @param ids 需要删除的预约时段类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePvBookingTimeByIds(Long[] ids) {
        pvBookingTimeMapper.deletePvBookingTimeInfoByTimeIds(ids);
        return pvBookingTimeMapper.deletePvBookingTimeByIds(ids);
    }

    /**
     * 删除预约时段类型信息
     *
     * @param id 预约时段类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePvBookingTimeById(Long id) {
        pvBookingTimeMapper.deletePvBookingTimeInfoByTimeId(id);
        return pvBookingTimeMapper.deletePvBookingTimeById(id);
    }

    /**
     * 新增预约时段详情列表信息
     *
     * @param pvBookingTime 预约时段类型对象
     */
    public void insertPvBookingTimeInfo(PvBookingTime pvBookingTime) {
        List<PvBookingTimeInfo> pvBookingTimeInfoList = pvBookingTime.getPvBookingTimeInfoList();
        Long id = pvBookingTime.getId();
        if (StringUtils.isNotNull(pvBookingTimeInfoList)) {
            List<PvBookingTimeInfo> list = new ArrayList<>();
            for (PvBookingTimeInfo pvBookingTimeInfo : pvBookingTimeInfoList) {
                pvBookingTimeInfo.setTimeId(id);
                list.add(pvBookingTimeInfo);
            }
            if (list.size() > 0) {
                pvBookingTimeMapper.batchPvBookingTimeInfo(list);
            }
        }
    }
}
